<b class="b1h"></b><b class="b2h"></b><b class="b3h"></b><b class="b4h"></b>
    <div class="headh">
        <h3>Stage 2</h3>
    </div>
    <div class="contenth">
        <div>
		Now we need your database details so we can setup your database and config file.  Please fill in your details below then click the test connection button to check if your details are correct or not.
		<br /><br />
		<?php
			if ($_POST['action']=="Test Connection") {
			$ok = 0;
				$hostname = $_POST['server'];
				$dbname = $_POST['dbname'];
				$username = $_POST['username'];
				$password = $_POST['password'];
			
				$conn = @mysql_connect($hostname, $username, $password);
				@mysql_select_db($dbname, $conn);
				
				if (!$conn) {
					//echo mysql_error();
					echo "<br /><br /><strong>The Connection Test Script was unable to connect to the MySQL server</strong>!<br />One or more of your input data is wrong<br /><br /><a href=\"index.php?p=2\">Go Back</a> and try again.";
				} else {
					?>
					<form action="index.php?p=2" method="POST">
						<input type="hidden" name="server" id="server" value="<?php echo $hostname;?>" />
						<input type="hidden" name="dbname" id="dbname" value="<?php echo $dbname;?>" />
						<input type="hidden" name="username" id="username" value="<?php echo $username;?>" />
						<input type="hidden" name="password" id="password" value="<?php echo $password;?>" />
						<table border="0" cellpadding="0" cellspacing="5">
							<tr>
								<td>Server hostname/IP address: </td><td> <?php echo $hostname;?></td>
							</tr>
							<tr>
								<td>Database Name: </td><td> <?php echo $dbname;?></td>
							</tr>
							<tr>
								<td>Username: </td><td> <?php echo $username;?></td>
							</tr>
							<tr>
								<td>Password: </td><td> <?php echo $password;?></td>
							</tr>
							<tr>
								<td colspan="2" align="center"><input type="submit" name="action" id="action" value="Install Database" /></td>
							</tr>
						</table>
					</form>
					<?php
				}
				
			} elseif ($_POST['action']=="Install Database") {
				echo "<strong>Importing Tables</strong><br /><br />";
				fcflush();
				$hostname = $_POST['server'];
				$dbname = $_POST['dbname'];
				$username = $_POST['username'];
				$password = $_POST['password'];
			
				$conn = @mysql_connect($hostname, $username, $password);
				@mysql_select_db($dbname, $conn);
				
				$file = "./pages/tables.php";
				$fh = fopen($file, 'r');
				$sql = fread($fh, filesize($file));
				fclose($fh);
				
				mysql_query("SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT");
				mysql_query("SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS");
				mysql_query("SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION");
				mysql_query("SET NAMES utf8");
				
				$queries = explode(" ;",$sql);
				for ($i=0;$i<count($queries)-1;$i++) {
					echo "Importing Table $i ";
					$result = mysql_query($queries[$i]) or die (mysql_error());
					echo " OK<br /><br />";
					fcflush();
				}
				echo "<strong>Table Creation Complete - Importing Data</strong><br /><br />";
				fcflush();
				$file = "./pages/data.php";
				$fh = fopen($file, 'r');
				$sql = fread($fh, filesize($file));
				fclose($fh);
				
				mysql_query("SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT");
				mysql_query("SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS");
				mysql_query("SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION");
				mysql_query("SET NAMES utf8");
				
				$queries = explode(";",$sql);
				for ($i=0;$i<count($queries)-1;$i++) {
					echo "Importing Dataset $i ";
					$result = mysql_query($queries[$i]) or die (mysql_error());
					echo " OK<br /><br />";
					fcflush();
				}
				
				if ($result) {
					$ok=1;
					echo "Database has been successfully imported please proceed to the next page.";		
					$_SESSION['dbdetails'] = $hostname.",".$dbname.",".$username.",".$password;
				} else {
					echo "There was an error importing your database.  Please contact support";
					$ok =0;
				}
			
			} else {
			$ok = 0;
		?>
		<form action="index.php?p=2" method="POST">
			<table border="0" cellpadding="0" cellspacing="5">
				<tr>
					<td>Server hostname/IP address: </td><td> <input type="text" name="server" id="server" /></td>
				</tr>
				<tr>
					<td>Database Name: </td><td> <input type="text" name="dbname" id="dbname" /></td>
				</tr>
				<tr>
					<td>Username: </td><td> <input type="text" name="username" id="username" /></td>
				</tr>
				<tr>
					<td>Password: </td><td> <input type="text" name="password" id="password" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="submit" name="action" id="action" value="Test Connection" /></td>
				</tr>
			</table>
		</form>
		<?php } ?>
		<br />
		</div>
    </div>
<b class="b4bh"></b><b class="b3bh"></b><b class="b2bh"></b><b class="b1h"></b>
<?php if ($ok==1) { ?>
<a href="index.php?p=3"><img src="next.png" border="0" align="right" /></a>
<?php } ?>
